#encoding: utf-8
class CertificateMailer < ActionMailer::Base
  default from: "gift@costumecode.ru"
  @@to = "certificate@costumecode.ru"
  def self.to
    @@to
  end

  def new_certificate(certificate)
    @certificate = certificate
    mail to: CertificateMailer.to, subject: 'Новый сертификат'
  end

  def certificate_paid(number)
    mail to: CertificateMailer.to, subject: 'Оплата сертификата' do |format|
      format.text { render text: "Сертификат №#{number} оплачен."}
    end
  end

  def certificate(certificate)
    add_inline_attachment!
    @name = certificate.friend_name
    @sender = certificate.name
    @date = (certificate.created_at+3.months).strftime("%d.%m.%Y")
    @number = certificate.order
    mail to: certificate.friend_email, subject: "Подарочный сертификат COSTUMECODE"
  end

  private

  def add_inline_attachment!
    set_inline_attachment('logo_0.png');
    set_inline_attachment('VectorSmartObject_0.png');
    set_inline_attachment('VectorSmartObject.png');
    set_inline_attachment('logo.png');
  end

  def set_inline_attachment(name)
    attachments.inline[name] = {content: File.binread(Rails.root.join("app/assets/images/#{name}")),
                                mime_type: "image/png"}
  end
end